//
//  BookDir.m
//  readNovels
//
//  Created by wulanzhou on 2017/2/15.
//  Copyright © 2017年 wulanzhou. All rights reserved.
//

#import "BookDir.h"
#import "CZFilesOperatoion.h"
#import "CZMyLog.h"
#import "CacheDataUtil.h"
#import "Catalog.h"

@implementation BookDir

+ (BookDir *)shareInstance
{
    static dispatch_once_t  onceToken;
    static BookDir * sSharedInstance;
    
    dispatch_once(&onceToken, ^{
        sSharedInstance = [[BookDir alloc] init];
    });
    return sSharedInstance;
}

- (instancetype)init{

    if (self = [super init]) {
        
    }
    return self;
}

- (NSString *)fileNameToPath:(NSString *)name{

    return [self.dirPath stringByAppendingPathComponent:name];
}

- (NSArray *)getAllFiles{

    NSArray *files = [[NSFileManager defaultManager] subpathsOfDirectoryAtPath:self.dirPath error:nil];
    return files;
}

//创建文件目录
- (void)createBookDirWithName:(NSString *)fileName{
    self.fileName = fileName;
    
    NSString *root=[CZFilesOperatoion getFilePathWithType:FileDirectoryDocumentType];
    NSString *path=[root stringByAppendingPathComponent:[fileName stringByDeletingPathExtension]];
    if (![CZFilesOperatoion createDirectoryWithPath:path]) {
        NSLog(@"创建目录失败!");
    }
    self.dirPath = path;
    NSLog(@"path =%@",path);
    self.filePath = [self.dirPath stringByAppendingPathComponent:self.fileName];
}

- (void)compressWithChpaters:(NSArray <Chapter *> *)chapters fileName:(NSString *)fileName{
    
    //创建保存目录
    [self createBookDirWithName:fileName];
    //生成文件
    NSLog(@"self.filePath =%@",self.dirPath);
    if (chapters && [chapters count] > 0) {
        
        [chapters enumerateObjectsUsingBlock:^(Chapter *obj, NSUInteger idx, BOOL * _Nonnull stop) {

            if (obj.content && [obj.content length]>0) {
                [CZMyLog writeLog:[NSString stringWithFormat:@"%@\n",obj.title] path:self.filePath];
                [CZMyLog writeLog:[NSString stringWithFormat:@"%@\n",[obj.content stringByReplacingOccurrencesOfString:obj.title withString:@""]] path:self.filePath];
            }
            
        }];
        
        NSLog(@"保存完成!");
    }
}

- (void)compress{
    
    NSLog(@"self.filePath =%@",self.dirPath);
    
    NSString *saveFilePath = [self.dirPath stringByAppendingPathComponent:@"完结版斗破苍穹.txt"];
    
    NSArray *catelogs = [CacheDataUtil unarchiveValueForKey:@"kBookCatalogsKey"];
    if (catelogs && [catelogs count] > 0) {
        [catelogs enumerateObjectsUsingBlock:^(Catalog *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSString *fileName =[NSString stringWithFormat:@"%d.%@.txt",idx+1,obj.name];
            NSString *path = [[BookDir shareInstance].dirPath stringByAppendingPathComponent:fileName];
            if([CZFilesOperatoion isFileExist:path]){
                NSString *content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
                [CZMyLog writeLog:content path:saveFilePath];
                NSLog(@"%@ 写入完成",fileName);
            }else{
                NSLog(@"%@  文件未找到",fileName);
                *stop=YES;
            }
            
        }];
        
    }

}



@end
